<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2015/6/16
 * Time: 21:40
 */

namespace common\models\Service;


use common\models\CommonFunction;
use common\models\DAO\User;
use common\models\DAO\UserCode;
use common\models\Http;
use common\msg\UserMsg;

class ForgetService {
    public static function set($data){
        $code = $data['code'];
        $tel = CommonFunction::encrypt($data['tel']);
        $user =  User::findOne(array('phone'=>$tel));
        $error['status'] = 0;
        $error['msg'] = UserMsg::$time_out;
        if(!$user){
            $error['msg'] = UserMsg::$not_found;
            return $error;
        }
        $today = time();
        $now = $today - 60;
        //array('condition' => 'tel=:state', 'params' => array(':state'=>$tel))
        $userCode = UserCode::find()->where(array('tel'=>$tel, 'code'=>$code, 'type'=>2))->andFilterWhere(['>', 'createTime', $now])->all();
        if(!$userCode){
            $error['msg'] = UserMsg::$send_error;
            return $error;
        }
        $userInfo = $user->attributes;
        $user->updateTime = $today;
        $user->loginPassword = CommonFunction::encryptPassword($data['password'], $userInfo['encrypt']);
        $user->updateTime = $today;
        $user->appKey = '';
        $result = $user->save();
        if($result){
            $userInfo = $user;
            $user_info = array('userID'=>$userInfo['id'], 'head'=>$userInfo['avatar'], 'avatar'=>$userInfo['avatar'], 'openID'=>$userInfo['openID'], 'tel'=>$data['tel'], 'name'=>$userInfo['realName'], 'recID'=>$userInfo['recID'], 'isNew'=>$userInfo['isNew']);
            Http::session('user', $user_info);
            return array('status'=>1);
        }
        else
            return $error;





    }
}